home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / ibnum.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  5.8 KB  |  217 lines

  1. /* Copyright (C) 1990, 1996, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: ibnum.c,v 1.2 2000/09/19 19:00:41 lpd Exp $ */
  20. /* Level 2 encoded number reading utilities for Ghostscript */
  21. #include "math_.h"
  22. #include "memory_.h"
  23. #include "ghost.h"
  24. #include "errors.h"
  25. #include "stream.h"
  26. #include "ibnum.h"
  27. #include "imemory.h"        /* for iutil.h */
  28. #include "iutil.h"
  29.  
  30. /* Define the number of bytes for a given format of encoded number. */
  31. const byte enc_num_bytes[] = {
  32.     enc_num_bytes_values
  33. };
  34.  
  35. /* ------ Encoded number reading ------ */
  36.  
  37. /* Set up to read from an encoded number array/string. */
  38. /* Return <0 for error, or a number format. */
  39. int
  40. num_array_format(const ref * op)
  41. {
  42.     switch (r_type(op)) {
  43.     case t_string:
  44.         {
  45.         /* Check that this is a legitimate encoded number string. */
  46.         const byte *bp = op->value.bytes;
  47.         int format;
  48.  
  49.         if (r_size(op) < 4 || bp[0] != bt_num_array_value)
  50.             return_error(e_rangecheck);
  51.         format = bp[1];
  52.         if (!num_is_valid(format) ||
  53.             sdecodeshort(bp + 2, format) !=
  54.             (r_size(op) - 4) / encoded_number_bytes(format)
  55.             )
  56.             return_error(e_rangecheck);
  57.         return format;
  58.         }
  59.     case t_array:
  60.     case t_mixedarray:
  61.     case t_shortarray:
  62.         return num_array;
  63.     default:
  64.         return_error(e_typecheck);
  65.     }
  66. }
  67.  
  68. /* Get the number of elements in an encoded number array/string. */
  69. uint
  70. num_array_size(const ref * op, int format)
  71. {
  72.     return (format == num_array ? r_size(op) :
  73.         (r_size(op) - 4) / encoded_number_bytes(format));
  74. }
  75.  
  76. /* Get an encoded number from an array/string according to the given format. */
  77. /* Put the value in np->value.{intval,realval}. */
  78. /* Return t_int if integer, t_real if real, t_null if end of stream, */
  79. /* or an error if the format is invalid. */
  80. int
  81. num_array_get(const ref * op, int format, uint index, ref * np)
  82. {
  83.     if (format == num_array) {
  84.     int code = array_get(op, (long)index, np);
  85.  
  86.     if (code < 0)
  87.         return t_null;
  88.     switch (r_type(np)) {
  89.         case t_integer:
  90.         return t_integer;
  91.         case t_real:
  92.         return t_real;
  93.         default:
  94.         return_error(e_rangecheck);
  95.     }
  96.     } else {
  97.     uint nbytes = encoded_number_bytes(format);
  98.  
  99.     if (index >= (r_size(op) - 4) / nbytes)
  100.         return t_null;
  101.     return sdecode_number(op->value.bytes + 4 + index * nbytes,
  102.                   format, np);
  103.     }
  104. }
  105.  
  106. /* Internal routine to decode a number in a given format. */
  107. /* Same returns as sget_encoded_number. */
  108. static const double binary_scale[32] = {
  109. #define EXPN2(n) (0.5 / (1L << (n-1)))
  110.     1.0, EXPN2(1), EXPN2(2), EXPN2(3),
  111.     EXPN2(4), EXPN2(5), EXPN2(6), EXPN2(7),
  112.     EXPN2(8), EXPN2(9), EXPN2(10), EXPN2(11),
  113.     EXPN2(12), EXPN2(13), EXPN2(14), EXPN2(15),
  114.     EXPN2(16), EXPN2(17), EXPN2(18), EXPN2(19),
  115.     EXPN2(20), EXPN2(21), EXPN2(22), EXPN2(23),
  116.     EXPN2(24), EXPN2(25), EXPN2(26), EXPN2(27),
  117.     EXPN2(28), EXPN2(29), EXPN2(30), EXPN2(31)
  118. #undef EXPN2
  119. };
  120. int
  121. sdecode_number(const byte * str, int format, ref * np)
  122. {
  123.     switch (format & 0x170) {
  124.     case num_int32:
  125.     case num_int32 + 16:
  126.         if ((format & 31) == 0) {
  127.         np->value.intval = sdecodelong(str, format);
  128.         return t_integer;
  129.         } else {
  130.         np->value.realval =
  131.             (double)sdecodelong(str, format) *
  132.             binary_scale[format & 31];
  133.         return t_real;
  134.         }
  135.     case num_int16:
  136.         if ((format & 15) == 0) {
  137.         np->value.intval = sdecodeshort(str, format);
  138.         return t_integer;
  139.         } else {
  140.         np->value.realval =
  141.             sdecodeshort(str, format) *
  142.             binary_scale[format & 15];
  143.         return t_real;
  144.         }
  145.     case num_float:
  146.         np->value.realval = sdecodefloat(str, format);
  147.         return t_real;
  148.     default:
  149.         return_error(e_syntaxerror);    /* invalid format?? */
  150.     }
  151. }
  152.  
  153. /* ------ Decode number ------ */
  154.  
  155. /* Decode encoded numbers from a string according to format. */
  156.  
  157. /* Decode a (16-bit, signed or unsigned) short. */
  158. uint
  159. sdecodeushort(const byte * p, int format)
  160. {
  161.     int a = p[0], b = p[1];
  162.  
  163.     return (num_is_lsb(format) ? (b << 8) + a : (a << 8) + b);
  164. }
  165. int
  166. sdecodeshort(const byte * p, int format)
  167. {
  168.     int v = (int)sdecodeushort(p, format);
  169.  
  170.     return (v & 0x7fff) - (v & 0x8000);
  171. }
  172.  
  173. /* Decode a (32-bit, signed) long. */
  174. long
  175. sdecodelong(const byte * p, int format)
  176. {
  177.     int a = p[0], b = p[1], c = p[2], d = p[3];
  178.     long v = (num_is_lsb(format) ?
  179.           ((long)d << 24) + ((long)c << 16) + (b << 8) + a :
  180.           ((long)a << 24) + ((long)b << 16) + (c << 8) + d);
  181.  
  182. #if arch_sizeof_long > 4
  183.     /* Propagate bit 31 as the sign. */
  184.     v = (v ^ 0x80000000L) - 0x80000000L;
  185. #endif
  186.     return v;
  187. }
  188.  
  189. /* Decode a float.  We assume that native floats occupy 32 bits. */
  190. float
  191. sdecodefloat(const byte * p, int format)
  192. {
  193.     bits32 lnum = (bits32) sdecodelong(p, format);
  194.     float fnum;
  195.  
  196. #if !arch_floats_are_IEEE
  197.     if (format != num_float_native) {
  198.     /* We know IEEE floats take 32 bits. */
  199.     /* Convert IEEE float to native float. */
  200.     int sign_expt = lnum >> 23;
  201.     int expt = sign_expt & 0xff;
  202.     long mant = lnum & 0x7fffff;
  203.  
  204.     if (expt == 0 && mant == 0)
  205.         fnum = 0;
  206.     else {
  207.         mant += 0x800000;
  208.         fnum = (float)ldexp((float)mant, expt - 127 - 23);
  209.     }
  210.     if (sign_expt & 0x100)
  211.         fnum = -fnum;
  212.     } else
  213. #endif
  214.     fnum = *(float *)&lnum;
  215.     return fnum;
  216. }
  217.